import axios, { AxiosResponse } from "axios";
import { AddressModel } from "../models/Address";
import { ElMessage } from "element-plus";
import { UserModel } from "../models/User";

class AddressApis {
    insertAddress(address: AddressModel): Promise<AddressModel> {
        let promise: Promise<AddressModel> = new Promise((resolve, reject) => {
            axios.post('/api/address/address', address)
                .then((reslut: AxiosResponse) => {
                    const code: number = reslut.data.code;
                    if (code == 200) {
                        const newAddress: AddressModel = reslut.data.data;
                        ElMessage.success('地址添加成功');
                        resolve(newAddress);
                    } else {
                        ElMessage.warning(reslut.data.msg);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        })
        return promise;
    }

    queryAddress(id: string): Promise<Array<AddressModel>> {
        let promise: Promise<Array<AddressModel>> = new Promise((resolve, reject) => {
            let user: UserModel = {
                id: id
            };
            axios.get('/api/address/address', { params: user })
                .then((reslut: AxiosResponse) => {
                    const code: number = reslut.data.code;
                    if (code == 200) {
                        const addressArr: Array<AddressModel> = reslut.data.data;
                        resolve(addressArr);
                    } else {
                        resolve([]);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        })
        return promise;
    }

    queryDefaultAddress(id: string): Promise<AddressModel|undefined> {
        let promise: Promise<AddressModel|undefined> = new Promise((resolve, reject) => {
            let address: AddressModel = {
                user: id
            };
            axios.get('/api/address/default', { params: address })
                .then((reslut: AxiosResponse) => {
                    const code: number = reslut.data.code;
                    if (code == 200) {
                        const address: AddressModel = reslut.data.data;
                        resolve(address);
                    } else {
                        resolve(undefined);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        })
        return promise;
    }

    deleteAddress(id: number) {
        let promise: Promise<boolean> = new Promise((resolve, reject) => {
            let address: AddressModel = {
                id: id
            };
            axios.delete('/api/address/address', { params: address })
                .then((reslut: AxiosResponse) => {
                    const code: number = reslut.data.code;
                    if (code == 200) {
                        ElMessage.success('地址删除成功');
                        resolve(true);
                    } else {
                        ElMessage.warning(reslut.data.error);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        })
        return promise;
    }

    updateAddress(id: number, userId: string): Promise<boolean> {
        let promise: Promise<boolean> = new Promise((resolve, reject) => {
            let address: AddressModel = {
                id: id,
                user: userId
            };
            axios.put('/api/address/address', address)
                .then((reslut: AxiosResponse) => {
                    const code: number = reslut.data.code;
                    if (code == 200) {
                        ElMessage.success('默认地址设置成功');
                        resolve(true);
                    } else {
                        ElMessage.warning(reslut.data.error);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        })
        return promise;
    }
}

export const addressApis: AddressApis = new AddressApis();